﻿using System;
using System.IO;
using System.Xml.Serialization;
using System.Xml;

namespace LAPI.Core.Helpers
{
    public class XmlHelper<T>
    {
        public static string XmlSerialize(T element)
        {
            XmlSerializer serializer = new XmlSerializer(element.GetType());
            TextWriter textWriter    = new StringWriter();

            /*textWriter.Encoding.BodyName.Normalize();

            string message;

            UTF8Encoding utf8 = new UTF8Encoding();

            Byte[] encodedBytes = utf8.GetBytes(textWriter);

            message = encodedBytes.ToString();

            /*System.Text.Encoding utf_8 = System.Text.Encoding.UTF8;
            byte[] utf8Bytes = utf_8.GetBytes(textWriter);

            string xmlFileUTF8 = System.Text.Encoding.UTF8.GetString(utf8Bytes);*/

            serializer.Serialize(textWriter, element);
            
            textWriter.Close();

            return textWriter.ToString().Replace("utf-16", "utf-8");
        }
        
        public static void XmlSerialize(T element, string xsdFile, string xmlFile)
        {
            XmlSerializer serializer = new XmlSerializer(element.GetType());
            TextWriter textWriter = new StreamWriter(xmlFile);          

            serializer.Serialize(textWriter, element);
            textWriter.Close();
        }

        public static string XmlSerialize(T element, string xsdFile)
        {
            XmlSerializer serializer = new XmlSerializer(element.GetType());
            StringWriter textWriter = new StringWriter();

            serializer.Serialize(textWriter, element);
            //textWriter.Close();

            return textWriter.ToString();
        }
        /*
        public static T XmlDeserializeFile(string fileName)
        {
            XmlSerializer deserializer = new XmlSerializer(typeof(T));
            TextReader textReader = new StreamReader(fileName);
            T ret = default(T);

            ret = (T)deserializer.Deserialize(textReader);
            textReader.Close();

            return ret;
        }
        */
        public static T XmlDeserialize(string xml)
        {
            XmlSerializer deserializer = new XmlSerializer(typeof(T));
            StringReader textReader = new StringReader(xml);
            T ret = default(T);
            ret = (T)deserializer.Deserialize(textReader);
            textReader.Close();
            return ret;
        }

        public static string XmlSerialize(object element)
        {
            XmlSerializer serializer = new XmlSerializer(element.GetType());
            TextWriter textWriter = new StringWriter();

            serializer.Serialize(textWriter, element);
            textWriter.Close();

            return textWriter.ToString();
        }

        public static object XmlDeserialize(Type type, string xml)
        {
            XmlSerializer deserializer = new XmlSerializer(type);
            StringReader textReader = new StringReader(xml);
            object ret = null;
            ret = deserializer.Deserialize(textReader);
            textReader.Close();
            return ret;
        }
    }
}
